package com.shihan._02_juc_concurrent_utils._02_sync_tools.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author shihan
 * @Date 2021/8/22 11:13
 * @Version 1.0
 */
public class MyCyclicBarrier implements Runnable {

    private CyclicBarrier cyclicBarrier;

    public MyCyclicBarrier(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + "：第一阶段开始执行...");
            TimeUnit.SECONDS.sleep(2);
            cyclicBarrier.await();

            System.out.println(Thread.currentThread().getName() + "：第二阶段开始执行...");
            TimeUnit.SECONDS.sleep(2);
            cyclicBarrier.await();

            System.out.println(Thread.currentThread().getName() + "：第三阶段开始执行...");
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName() + "：第三阶段执行结束！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
